package com.erp.auto.service.impl;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.erp.auto.domain.ErpAutoGoldCoinConfig;
import com.erp.auto.mapper.ErpAutoGoldCoinConfigMapper;
import com.erp.auto.mapper.ErpAutoSellerStoreConfigMapper;
import com.erp.auto.service.ErpAutoGoldCoinConfigService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.shop.api.RemoteSellerStoreService;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 自动化店铺金币配置
 *
 * @author WXX
 * @date 2024-01-25 16:50:03
 */
@Service
@AllArgsConstructor
public class ErpAutoGoldCoinConfigServiceImpl extends ServiceImpl<ErpAutoGoldCoinConfigMapper, ErpAutoGoldCoinConfig> implements ErpAutoGoldCoinConfigService {

    private final ErpAutoGoldCoinConfigMapper erpAutoGoldCoinConfigMapper;
    private final RemoteSellerStoreService sellerStoreService;
    @Override
    public R<Object> saveGoldCoin(ErpAutoGoldCoinConfig config) {
        Long storeId = config.getStoreId();
        String cookie = sellerStoreService.getCookieByStoreId(storeId);
        if (cookie == null) {
            return R.fail("cookie为空");
        }
//        String cookie = "ali_apache_id=33.8.71.47.1700632090424.886830.8; _gcl_au=1.1.1040922642.1700632095; _ym_uid=1700632097384053663; _ym_d=1700632097; account_v=1; _hvn_login=13; _fbp=fb.1.1701322249259.426829522; e_id=pt30; aep_common_f=I363L4dJXx8bSxl+okgWJbg7XnqoyXsJ0KHztNgVsnbxwbgCgmGYfw==; _bl_uid=6al5Ipzwtzw6dk9kkdmz8eCvae3F; aeu_cid=52f5c35ec59b4852bb315adba5cc2bbb-1702542900630-03908-cX37Vqz6; u_info=fh5fJzfrf5ikL4BJ2356ExeUlJG+ikyK9p7kpnXQyqY=; Hm_lvt_ed0a6497a1fdcdb3cdca291a7692408d=1702965475; Hm_lvt_7afe580efa9cda86356bdea8077a83e7=1702965476; x_router_us_f=x_alimid=2674993116; cna=8KgeHm6bxHoCAXjgdy5NzQnb; l=fB_xbMDHPuYjQxWJBO5IKurza7796BdbcPVzaNbMiIEGa6M5qI205NCOpcOXndtjgTfVhe-rWDM-1dE6JXaLRjkDBeYI4OGGBRJ9-iRLS45..; lzd_cid=b8f9769f-4cc1-430d-b5f0-32f762cbf515; lazop_lang=zh_CN; havana_tgc=NTGC_1c26353408914f557f5014ad81652c0c; xman_us_t=x_lid=cn1075970114vjnae&sign=y&rmb_pp=86-19531943413&x_user=FcKss4OAi86W4eE0b45A51+vgsUK9Yk/ihq1bPDGSa4=&ctoken=1ausaprre07bp&l_source=aliexpress; xman_f=Zu/gpEqjkUt3de76XZo+ljyQ0+7j058iQHsWCI7kujAj+wF7ZF2f1QdNZD1URe6nh/9i3PHTrjkX8f106IGaQBXBjtCpiEGBupdZh1haOtsEe04wc76AC9t2uFs4QH48/U2sop1Dy51L/VVZSvzH1nD6kQ53YCETznWVQzJx61gX14IVcfA6gm8OBJ58tmKJfIxIG+Tnn4j7yDrjFBteq+zl8d/xUOQgw3bBgtsyDLpjPSIiVQLm2zzlgyWSXFAYlh/b749AiG9rBWWJ+1GFJTRfD6bUaGccG+2QMD+WzqWhFghrGwQktObqlAAYLXAS42GQga8ZvC/CqgpHyKqthT20v5We7ycIbgfzqc6NsOIV4k5+rGpC3oWQP3TxGTufuagN0v4m/1v1EBBpi49B1mWy3sK/uEBZ+ARlalIrFM/togRcKWxXqL5whU8yLLUZg5QsuAIi3wXn+uc1xbE97XfOTmXdrqEO; xman_us_f=zero_order=y&x_locale=en_US&x_l=0&x_user=CN|default%20firstName|default%20lastName|cnfm|2674993116&x_lid=cn1075970114vjnae&x_c_chg=0&x_as_i=%7B%22aeuCID%22%3A%2252f5c35ec59b4852bb315adba5cc2bbb-1702542900630-03908-cX37Vqz6%22%2C%22affiliateKey%22%3A%22cX37Vqz6%22%2C%22channel%22%3A%22AFFILIATE%22%2C%22cv%22%3A%221%22%2C%22isCookieCache%22%3A%22N%22%2C%22ms%22%3A%220%22%2C%22pid%22%3A%22143472817%22%2C%22tagtime%22%3A1702542900630%7D&acs_rt=ceeddf0591f74dd49e0f0c3daba86ea2; aep_usuc_f=isfm=y&site=glo&c_tp=USD&x_alimid=2674993116&iss=y&s_locale=zh_CN&region=CN&b_locale=en_US; acs_usuc_t=x_csrf=bayrdvpujbrn&acs_rt=193fa09d8e8d408997282a12b39658e7; gmp_sid=2674993116; xlly_s=1; XSRF-TOKEN=ae3ae68e-6f9a-4c46-9548-032166d48cd0; AB_DATA_TRACK=472051_617388; AB_ALG=; AB_STG=st_StrategyExp_1694492533501%23stg_687; intl_locale=en_US; aep_history=keywords%5E%0Akeywords%09%0A%0Aproduct_selloffer%5E%0Aproduct_selloffer%091005006011897852%091005005865422085%0932827868245%091005002990244740%0932845497995%091005006260559936%091005005666899133%091005005892114558; _gid=GA1.2.777384385.1705907860; _ym_isad=2; JSESSIONID=8C58B68FAAD2585B1227900146401025; shop_channel=ae; _mle_tmp_harden0=COMyBPKnGxIg1PC5i8Kdr3TDud6MK9%2FxNleFpR5gLkAwcG4a%2FrsKvS1hr2oNn7acXnvMhnuj%2B2PxQ17bpYpFOhiSGd1eObCqMfT4lEgl7zh0Lo2tDGe4B6i1zFIKKBfK; _lang=zh_CN; _m_h5_tk=9763716f3681cc7dd03cfa544ed757f6_1705981444203; _m_h5_tk_enc=02a09103f4645a7c763b8bc726052ab4; _gat=1; _ga_VED1YSGNC7=GS1.1.1705979286.37.1.1705979766.60.0.0; _ga=GA1.1.151884950.1700632095; x5sec=7b22617365727665722d696e746c3b32223a226638316261613663303335306432366666313436396430643534323838333230434f3765764b3047454a7543717138484d5036503668513d222c22733b32223a2232653130653963353266653834303062227d; intl_common_forever=Qp6ikJqbzChLYsFrxJ6aFpPTzMF6YSKatnNJlPHiCuFY5z09cxjLYg==; xman_t=nnLekPRUCLx/aQCywEvbYtycRJDu0iGYtWw7ZF2aDUnAPzOtucAYVNQYCcNT/7wT0hJWv4s4qKdvWW8969tKnkqZ2sGDtKbUyq8VQUTAXMJ+LcqW6uBs44sDvKFVoTfEA12rB4ehpLTdXLGSiQVI52bWsFRcdehhEQAhKmPlsFIkiWiVzJSvJj9CbqEzX/k2LhkkFsroV6INHjgeiYBpQ2yDjq5Ackbzzzbx0h+XNIfk34chfG1qMRnfNi7xhQ1LdX0sBA/AmoZs/AF74SklnTKk1xK+o6FMcQ4RkKnQWLjaqboh8K/0/UxOkNdY8LVrfVj5rDbKi8WgxR5DvRe+EAQpB7gQhwbw2tMBVOoDJj4UA9W+7cKfWOd8zkdN4sUQHmIndSBI3HlpN6w8HuDZKcJ+IlFh5la8GEvgrP40GQcGpiN3XBvvyMGTbeOzxxjC0KtesOR+6W7SMMe67Ad4iHIx4MNjlUYqbnXsBs1WMXwHLDjCKiLYysV+42MO2ZtgU3OtaTJSrI8tqMcO2wgp11MbGd8cz4pCg_FwHH_X2fsyh6fpLpmHvFDbyzHZQKTPlFzlh-vcVu8JxrFtQAJzzUK3yzHZQKTyyH4YDAkwUz5..; isg=BPPzpbm6kjUeZ16TA97hdWJMgvcdKIfqBKnI-KWQRZJLpBNGLfzSOV02WtRKVd_i; cto_bundle=Pfk9EF91WlFZN1dJJTJGcUZsQjlZWjIzVGI3aERmWUs3cVhjdGRXbGxGVGg3TXhXanpDV0NEQkdmWGdMMWtZT0hrRDJFQTM5V3FYYWY3cGh3WGFIcFFIM2UlMkZTQkVlYjQzS2R0Q1k4bSUyRkhBMjdETHJMSjNCd3FhYmVuaGF3UzFtTkFEREsxcXUlMkZnNDI4Q2dibWdQQ1SNG1XRk5lZyUzCUzRA";

        RestTemplate restTemplate = new RestTemplate();
        Map<String, Object> map = new HashMap<>();
        map.put("cookie_str", cookie);
        map.put("discount", config.getPlatformDiscount().toString());
        ResponseEntity<JSONObject> responseEntity = null;
        try {
            responseEntity = restTemplate.postForEntity("http://47.94.131.193:8000/set/goldDiscount", map, JSONObject.class);
        }catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
        JSONObject body = responseEntity.getBody();
        Integer code = body.getInt("code");
        if (code == 200) {
            String str = body.getJSONObject("data").getJSONArray("ret").getStr(0);
            if (!str.contains("SUCCESS"))
                return R.fail(str);
            config.setCreateTime(new Date());
            int save = 0;
            if (config.getId() == null) {
                save = erpAutoGoldCoinConfigMapper.insert(config);
            }else {
                save = erpAutoGoldCoinConfigMapper.updateById(config);
            }
            if (save > 0) {
                return R.ok();
            }else {
                return R.fail();
            }
        }
        return R.fail(body.getStr("msg"));
    }
}
